<?php
/**
 * 提现model
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class b2c_mdl_salesorderReport extends dbeav_model{



    // var $defaultOrder = array('m.member_id', ' ASC');
    // 关联dbschema
    function __construct($app)
    {
        parent::__construct($app);
        $this->app = $app;

        $member_lvmodel=app::get('b2c')->model('member_lv');
        $member_lvdata=$member_lvmodel->getList('name,member_lv_id');
        $member_lv=array();
        for ($i=0; $i <count($member_lvdata) ; $i++) { 
            $member_lv[$member_lvdata[$i]['name']]=$member_lvdata[$i]['name'];
        }

        if(!$member_lv)
        {
            $member_lv=0;
        }

        $columns=array(
                    'name'=>array('label'=>app::get('b2c')->_('活动名称')
                        ,'width'=>200),
                    'ordertime'=>array('label'=>app::get('b2c')->_('活动时限')
                        ,'width'=>200),

                     'member_lv_ids'=>array('label'=>app::get('b2c')->_('渠道商级别'),'width'=>1000),
                    );

        $this->columns = array(
                    'name'=>array('label'=>app::get('b2c')->_('活动名称')
                        ,'width'=>200),
                    'ordertime'=>array('label'=>app::get('b2c')->_('活动时限')
                        ,'width'=>200),
                     'member_lv_ids'=>array('label'=>app::get('b2c')->_('渠道商级别'),'width'=>1000),


                     'from_time'=>array('label'=>app::get('b2c')->_('起始时间'),'filtertype' => 'normal','filterdefault' => 'true','type'=>'date'),
                    'to_time'=>array('label'=>app::get('b2c')->_('截止时间'),'filtertype' => 'normal','filterdefault' => 'true','type'=>'date'),
                     'searchmember_lv_ids'=>array('label'=>app::get('b2c')->_('渠道商级别'),'filtertype' => 'normal','filterdefault' => 'true','width'=>400,'type'=>$member_lv),
            );
        $this->schema = array(
            'default_in_list'=>array_keys($columns),
            'in_list'=>array_keys($columns),
            'idColumn'=>'name',
            'columns'=>$this->columns
        );
    }
    
    

    function get_schema(){
        return $this->schema;
    }

 
 public function _filter($filter,$tableAlias=null,$baseWhere=null){
        $where = array(1);
        foreach ($filter as $key => $value) {
            switch ($key) {
                case 'from_time':
                    $from_time=strtotime($value);
                    $where[]="'$from_time' <= a.from_time";
                    break;
                case 'to_time':
                    $to_time=strtotime($value." 23:59:59");
                    $where[]="'$to_time' >= a.to_time";
                    break;
                // case 'searchmember_lv_ids':
                //     $where[]="a.member_lv_ids like '%$value%'";
                //     break;
                
            }
        }
        return implode($where,' AND ');
    }


    function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null)
    {       
        
        $member_lvmodel=app::get('b2c')->model('member_lv');
        $sql="select a.`name`,a.from_time,a.to_time,a.member_lv_ids
                from sdb_b2c_sales_rule_order a
                where 1=1";
        if($sql!='')
        {
            $sql.=' and '.$this->_filter($filter);
            $data=$this->db->selectLimit($sql,$limit,$offset);
            foreach ($data as $key=>$value)
            {
                
               $data[$key]["name"]=$value["name"];
                $data[$key]["ordertime"]=date("Y-m-d",$value["from_time"]).'~'.date("Y-m-d",$value["to_time"]);

                $member_lv_ids = explode(',',$value["member_lv_ids"]);
                $member_lvdata=$member_lvmodel->getList('name',array('member_lv_id'=>$member_lv_ids));
                for ($i=0; $i <count($member_lvdata) ; $i++) { 
                    if($i>=1)
                    {
                        $data[$key]["member_lv_ids"].=','.$member_lvdata[$i]['name'];
                    }
                    else
                    {
                         $data[$key]["member_lv_ids"]=$member_lvdata[$i]['name'];
                    }
                }
                if(isset($filter['searchmember_lv_ids']))
                {
                    if(strpos($data[$key]["member_lv_ids"], $filter['searchmember_lv_ids']))
                    {

                    }
                    else
                    {
                        unset($data[$key]);
                    }
                }

                $key++;
            }
        }
        return $data;

    
}

    function count($filter=null)
    {
        $count=0;
        $member_lvmodel=app::get('b2c')->model('member_lv');
        $sql="select a.`name`,a.from_time,a.to_time,a.member_lv_ids
                from sdb_b2c_sales_rule_order a
                where 1=1";
        if($sql!='')
        {
            $sql.=' and '.$this->_filter($filter);
            $data=$this->db->select($sql);

            foreach ($data as $key=>$value)
            {
                $member_lv_ids = explode(',',$value["member_lv_ids"]);
                $member_lvdata=$member_lvmodel->getList('name',array('member_lv_id'=>$member_lv_ids));
                for ($i=0; $i <count($member_lvdata) ; $i++) { 
                    if($i>=1)
                    {
                        $data[$key]["member_lv_ids"].=','.$member_lvdata[$i]['name'];
                    }
                    else
                    {
                         $data[$key]["member_lv_ids"]=$member_lvdata[$i]['name'];
                    }
                }
                if(isset($filter['searchmember_lv_ids']))
                {
                    if(strpos($data[$key]["member_lv_ids"], $filter['searchmember_lv_ids']))
                    {

                    }
                    else
                    {
                        unset($data[$key]);
                    }
                }

                $key++;
            }


            $count=count($data);
        }

        return $count;
    }
}
